import {
    Dispatch
} from 'redux';

import ajax from './../../http';

import * as types from './../types/common';

export default {
    //获取公用数据
    [types.getCommonData]() {
        return async (dispatch: Dispatch) => {
            try {
                const  res = await (
                    ajax.get(`common/data`)
                )
                dispatch({
                    data:res,
                    type:types.getCommonData
                })
            }
            catch (e) {
                return Promise.reject(e)
            }
        }
    },
    //喜欢某个用户
    [types.postLikeUser](
        userId: string,
        remove:boolean = true
    ) {
        return async (dispatch: Dispatch) => {
            const _params = {
                what: `user`,
                id: userId
            }
            const _url = (
                `like/user/${userId}`
            )
            try {
                const res = (
                    await ajax.post(_url, _params)
                )
                dispatch({
                    type: types.postLikeUser,
                    data: {
                        remove:remove,
                        userId: userId
                    }
                })
                return res;
            }
            catch (e) {
                return Promise.reject(e);
            }
        }
    },
    //不喜欢某个用户
    [types.postUnlikeUser](
        userId:number,
        remove:boolean = true
    ) {
        return async (dispatch: Dispatch) => {
            const _params = {
                what: `user`,
                id: userId
            }
            const _url = (
                `unlike/user/${userId}`
            )
            try {
                const res = (
                    await ajax.post(_url, _params)
                )
                dispatch({
                    type: types.postUnlikeUser,
                    data: {
                        remove:remove,
                        userId: userId
                    }
                })
                return res;
            }
            catch (e) {
                return Promise.reject(e);
            }
        }
    },
    //重置喜欢
    [types.resetLikeUser](userId: string) {
        return async (dispatch: Dispatch) => {
            const _params = {
                userId: userId
            }
            const _url = `like/user/status`;
            try {
                await ajax.put(_url, _params);
                dispatch({
                    type: types.resetLikeUser,
                    data: {
                        userId: userId
                    }
                })
            }
            catch (e) {
                return Promise.reject(e);
            }
        }
    },
    //屏蔽某个用户
    [types.blockUser](userId:string) {
        return async (dispatch: Dispatch) => {
            const _url = `block/user/${userId}`;
            try {
                await ajax.post(_url);
                dispatch({
                    type: types.blockUser,
                    data: {
                        userId: userId
                    }
                })
            }
            catch (e) {
                return Promise.reject(e);
            }
        }
    },
    //获取report列表
    [types.getReportTypes]() {
        return async (dispatch: Dispatch) => {
            try {
                const list = (
                    await ajax.get(
                        `report/types`
                    )
                )
                dispatch({
                    data:list,
                    type:types.getReportTypes
                })
                return list;
            }
            catch (e) {
                return Promise.reject(e);
            }
        }
    }
}